<?php
Yii::import('ext.image.*');
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class AController extends RController
{
	public $jquery10 = false;
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public $defaultAction = 'admin';
	public $title ='';
	
	/**
	 * 
	 * @param type $objs
	 * @param type $modelf
	 * 
	 * fungsi untuk mengsavephoto
	 */
	public function savePhotos($attributes = array(),$model,$path){
		foreach($attributes as $attribute){
			$tmpFileName = $model->$attribute;
			$tmpObj = CUploadedFile::getInstance($model, $attribute);
			$location = Yii::app()->params[$path];
			if($tmpObj !== null){
				$filename = $this->getFilename($tmpObj->getName(),get_class($model)."_".$model->id,$attribute);
				$model->$attribute = $filename;
				if (!$model->isNewRecord) {
					$model->saveAttributes(array($attribute));
					$tmpObj->saveAs("$location/$filename");
				}
			}
		}
	}
	
	/**
	 * 
	 * @param type $attributes
	 * @param type $model
	 * @param type $path
	 * @param type $height
	 * @param type $width
	 * 
	 * Fungsi untuk meng-save foto dengan resize ukuran
	 */
	public function savePhotosResize($attributes=array(),$model,$path,$height=800,$width=600, $ratio = Image::AUTO){
		$this->savePhotos($attributes,$model,$path);
		foreach($attributes as $attr){
			if(empty($model->$attr)){
				continue;
			}
			$imageLocation = $model->getPicturePath($attr,$path);
			if (file_exists($imageLocation) && is_file($imageLocation)) {
				$image = Yii::app()->image->load($imageLocation);
				$image->resize($height,$width,$ratio);
				$image->save();
			}
		}
	}
	
	/**
	 * Men-generate filename untuk file yang di upload
	 * spasi pada filename diganti dengan underscore
	 */
	protected function getFileName($name, $modelId, $attribute){
		$name = str_replace(" ", "_", $name);
		return $filename = "{$modelId}_{$attribute}_{$name}";
	}
	
	protected function sendEmail($emailAddress,$subject,$content){
		$mail = new YiiMailer();
		
		$emailAdmin = Option::model()->findByAttributes(array('name'=>'email_admin'));
		$emailName = Option::model()->findByAttributes(array('name'=>'email_name'));
		
		if(empty($emailAdmin)){
			$emailAdmin = Yii::app()->params['email_admin'];
		}else{
			$emailAdmin = $emailAdmin->value;
		}
		if(empty($emailName)){
			$emailName = Yii::app()->params['email_name'];
		}
		else{
			$emailName = $emailName->value;
		}
		
		$mail->setFrom($emailAdmin, $emailName);
		$mail->setTo($emailAddress);
		$mail->setSubject($subject);
		$mail->setBody($content);
		$mail->send();
	}
	
	public function sidemenu(){
		$menu = $this->menu;
		if(empty($menu)){
			echo '<div class="subnav"><div class="subnav-content">
					<h4>There is no operation in this page.</h4>
				  </div></div>';
		}else{

			echo '<div class="subnav">';
			echo '
				<div class="subnav-title">
					<a href="#" class="toggle-subnav">
						<i class="fa fa-angle-down"></i>
						<span>Operation</span>
					</a>
				</div>
			';

			$this->widget('zii.widgets.CMenu', array(
				'items'=>$this->menu,
				'htmlOptions'=>array(
					'class'=>'subnav-menu',
				)
			));
			
			echo '</div>';
		}
	}
	
	public function breadcrumb(){
		if (isset($this->breadcrumbs)){
			$this->widget('zii.widgets.CBreadcrumbs', array(
				'links' => $this->breadcrumbs,
				'htmlOptions'=>array('class'=>'breadcrumb'),
			));
		}
	}
	
	public function filter()
	{
		return array(
			'rights',
		);
	}
	
	protected function sendEmailStatusChanged($emailAddress,$subject,$content,$applicantName,$prevStep,$nextStep,$targetPosition){
		$mail = new YiiMailer();
		
		$emailAdmin = Option::model()->findByAttributes(array('name'=>'email_admin'));
		$emailName = Option::model()->findByAttributes(array('name'=>'email_name'));
		
		if(empty($emailAdmin)){
			$emailAdmin = Yii::app()->params['email_admin'];
		}else{
			$emailAdmin = $emailAdmin->value;
		}
		if(empty($emailName)){
			$emailName = Yii::app()->params['email_name'];
		}
		else{
			$emailName = $emailName->value;
		}
		
		$mail->setFrom($emailAdmin,$emailName);
		$mail->setTo($emailAddress);
		$mail->setSubject($subject);
		$content = $content['value'];
		eval("\$content = \"$content\";");
		$mail->setBody($content);
		$mail->send();
	}
	
	protected function saveEmailQueue($emailAddress,$subject,$content,$applicantName,$prevStep,$nextStep,$targetPosition){
		$content = $content['value'];
		eval("\$content = \"$content\";");
		
		$model = new CronjobEmail();
		$model->email_to = $emailAddress;
		$model->subject = $subject;
		$model->content = $content;
		$model->sent = 0;
		$model->save(false);
	}
}